<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MyDy后端服务 - 状态监控</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: 'Helvetica Neue', Arial, sans-serif;
            line-height: 1.6;
            color: #333;
            background-color: #f7f9fc;
            padding: 20px;
        }
        .container {
            max-width: 1000px;
            margin: 0 auto;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
            overflow: hidden;
        }
        header {
            background-color: #2196F3;
            color: white;
            padding: 20px;
            text-align: center;
        }
        h1 {
            font-size: 24px;
            font-weight: 500;
        }
        .status-card {
            padding: 20px;
            margin: 20px;
            border-radius: 5px;
            background-color: #ebf7ff;
            border-left: 4px solid #2196F3;
        }
        .endpoints {
            padding: 20px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }
        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #e0e0e0;
        }
        th {
            background-color: #f5f5f5;
            font-weight: 500;
        }
        tr:hover {
            background-color: #f9f9f9;
        }
        .method {
            display: inline-block;
            padding: 3px 6px;
            border-radius: 3px;
            font-size: 12px;
            font-weight: bold;
            color: white;
        }
        .get {
            background-color: #61affe;
        }
        .post {
            background-color: #49cc90;
        }
        footer {
            text-align: center;
            padding: 20px;
            color: #666;
            font-size: 14px;
            border-top: 1px solid #eee;
        }
        .status-indicator {
            display: inline-block;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            margin-right: 5px;
        }
        .online {
            background-color: #4CAF50;
        }
        .offline {
            background-color: #F44336;
        }
        pre {
            background-color: #f5f5f5;
            padding: 15px;
            border-radius: 5px;
            overflow-x: auto;
        }
        code {
            font-family: 'Consolas', monospace;
        }
        @media (max-width: 768px) {
            .container {
                border-radius: 0;
            }
            table {
                font-size: 14px;
            }
            th, td {
                padding: 8px 10px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>MyDy后端服务</h1>
            <p>API状态监控面板</p>
        </header>
        
        <div class="status-card">
            <h2><span class="status-indicator" id="statusDot"></span> <span id="statusText">正在检查状态...</span></h2>
            <p>上次检查时间: <span id="lastCheck">-</span></p>
            <p>服务器时间: <span id="serverTime">-</span></p>
            <p>服务器运行时间: <span id="uptime">-</span></p>
            <p>数据库状态: <span id="dbStatus">-</span></p>
        </div>
        
        <div class="endpoints">
            <h2>可用API端点</h2>
            <table>
                <thead>
                    <tr>
                        <th>方法</th>
                        <th>路径</th>
                        <th>描述</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><span class="method post">POST</span></td>
                        <td>/register</td>
                        <td>用户注册</td>
                    </tr>
                    <tr>
                        <td><span class="method post">POST</span></td>
                        <td>/login</td>
                        <td>用户登录</td>
                    </tr>
                    <tr>
                        <td><span class="method get">GET</span></td>
                        <td>/user/info</td>
                        <td>获取用户信息</td>
                    </tr>
                    <tr>
                        <td><span class="method get">GET</span></td>
                        <td>/users</td>
                        <td>获取用户数据</td>
                    </tr>
                    <tr>
                        <td><span class="method get">GET</span></td>
                        <td>/advertisements</td>
                        <td>获取广告列表</td>
                    </tr>
                    <tr>
                        <td><span class="method get">GET</span></td>
                        <td>/tasks/reward</td>
                        <td>获取任务奖励</td>
                    </tr>
                    <tr>
                        <td><span class="method get">GET</span></td>
                        <td>/tasks/user/status</td>
                        <td>获取用户任务状态</td>
                    </tr>
                    <tr>
                        <td><span class="method get">GET</span></td>
                        <td>/health</td>
                        <td>API健康状态检查</td>
                    </tr>
                    <tr>
                        <td><span class="method get">GET</span></td>
                        <td>/api/logs/:type</td>
                        <td>获取指定类型的日志内容（仅管理员）</td>
                    </tr>
                    <tr>
                        <td><span class="method get">GET</span></td>
                        <td>/api/logs/stats/summary</td>
                        <td>获取日志统计信息（仅管理员）</td>
                    </tr>
                </tbody>
            </table>
            
            <h2>管理功能</h2>
            <p>管理员可以通过访问 <a href="/admin-logs.html">日志管理页面</a> 查看和监控系统日志。</p>
            
            <h2>API使用示例</h2>
            <pre><code>// 登录示例
fetch('/login', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        username: 'qifeng',
        password: 'admin123'
    })
})
.then(response => response.json())
.then(data => console.log(data));</code></pre>
        </div>
        
        <footer>
            <p>MyDy后端服务 &copy; 2024</p>
            <p>部署于宝塔面板</p>
        </footer>
    </div>
    
    <script>
        // 检查API状态
        function checkApiStatus() {
            fetch('/health')
                .then(response => response.json())
                .then(data => {
                    const statusDot = document.getElementById('statusDot');
                    const statusText = document.getElementById('statusText');
                    const lastCheck = document.getElementById('lastCheck');
                    const serverTime = document.getElementById('serverTime');
                    const uptime = document.getElementById('uptime');
                    const dbStatus = document.getElementById('dbStatus');
                    
                    // 更新状态指示器
                    if (data.status === 'ok') {
                        statusDot.className = 'status-indicator online';
                        statusText.textContent = 'API在线运行中';
                    } else {
                        statusDot.className = 'status-indicator offline';
                        statusText.textContent = 'API服务异常';
                    }
                    
                    // 更新时间信息
                    lastCheck.textContent = new Date().toLocaleString();
                    serverTime.textContent = new Date(data.time).toLocaleString();
                    
                    // 更新运行时间
                    const uptimeSeconds = data.uptime;
                    const days = Math.floor(uptimeSeconds / 86400);
                    const hours = Math.floor((uptimeSeconds % 86400) / 3600);
                    const minutes = Math.floor((uptimeSeconds % 3600) / 60);
                    const seconds = Math.floor(uptimeSeconds % 60);
                    
                    let uptimeText = '';
                    if (days > 0) uptimeText += `${days}天 `;
                    if (hours > 0 || days > 0) uptimeText += `${hours}小时 `;
                    if (minutes > 0 || hours > 0 || days > 0) uptimeText += `${minutes}分钟 `;
                    uptimeText += `${seconds}秒`;
                    
                    uptime.textContent = uptimeText;
                    
                    // 更新数据库状态
                    dbStatus.textContent = data.database === 'connected' ? '已连接' : '未连接';
                    if (data.database === 'connected') {
                        dbStatus.style.color = '#4CAF50';
                    } else {
                        dbStatus.style.color = '#F44336';
                    }
                })
                .catch(error => {
                    console.error('API状态检查失败:', error);
                    const statusDot = document.getElementById('statusDot');
                    const statusText = document.getElementById('statusText');
                    statusDot.className = 'status-indicator offline';
                    statusText.textContent = 'API服务不可用';
                });
        }
        
        // 页面加载时检查状态
        checkApiStatus();
        
        // 每30秒自动刷新状态
        setInterval(checkApiStatus, 30000);
    </script>
</body>
</html> 